home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Graphics / PostScript / Bezier / Source / Bezier.m < prev    next >
Encoding:
Text File  |  1995-06-12  |  2.6 KB  |  115 lines

  1.  
  2. /* Generated by Interface Builder */
  3.  
  4. #import "Bezier.h"
  5. #import <appkit/Window.h>
  6. #import <appkit/Control.h>
  7. #import <appkit/Application.h>
  8. #import <dpsclient/psops.h>
  9. #import <dpsclient/wraps.h>
  10. #import <stdlib.h>
  11. #import <math.h>
  12.  
  13. @implementation Bezier
  14.  
  15. + newFrame:(NXRect *)r
  16. {
  17.   int i;
  18.   self = [super newFrame:r];
  19.   p[ 0].x=50.0;            // init all the variables.
  20.   p[ 0].y=200.0;
  21.   p[ 1].x=150.0;
  22.   p[ 1].y=300.0;
  23.   p[ 2].x=250.0;
  24.   p[ 2].y=100.0;
  25.   p[ 3].x=350.0;
  26.   p[ 3].y=200.0;
  27.   lineWidth=2.0;
  28.   blockSize=4.0;
  29.   return self;
  30. }
  31.  
  32. -(BOOL)acceptsFirstMouse    // accept first mouseDown.
  33. {
  34.   return YES;
  35. }
  36.  
  37. - setLineWidth:(double)width
  38. {
  39.   lineWidth=width;
  40.   return [self display];
  41. }
  42. - setBlockSize:(double)size
  43. {
  44.   blockSize=size;
  45.   return [self display];
  46. }
  47. - takeLineWidthFrom:sender
  48. {
  49.   return [self setLineWidth:[sender doubleValue]];
  50. }
  51. - takeBlockSizeFrom:sender
  52. {
  53.   return [self setBlockSize:[sender doubleValue]];
  54. }
  55. -(double)blockSize
  56. {
  57.   return blockSize;
  58. }
  59. -(double)lineWidth
  60. {
  61.   return lineWidth;
  62. }
  63.  
  64. - mouseDown:(NXEvent *)e
  65. {
  66.   NXPoint mLoc;
  67.   int j;
  68.   int saveMask;
  69.   
  70.   mLoc=e->location;                // get location.
  71.   [self convertPoint:&mLoc fromView:nil];    // convert to local View.
  72.   for( j=0; j<4; j++)                // is it a control point?
  73.     if( fabs( mLoc.x-p[ j].x)<=blockSize/2.0
  74.      && fabs( mLoc.y-p[ j].y)<=blockSize/2.0)
  75.       break;                    // break out of loop.
  76.   if( j<4)                    // found a point,
  77.     {                        // change the event mask.
  78.       saveMask=[[self window] addToEventMask:(NX_MOUSEUPMASK |
  79.                           NX_MOUSEDRAGGEDMASK)];
  80.       [self lockFocus];
  81.       do {
  82.         mLoc=e->location;            // each time, get location,
  83.     [self convertPoint:&mLoc fromView:nil];    // and convert.
  84.     p[ j]=mLoc;                // set the right control point.
  85.     [self drawSelf:&bounds :1];        // redisplay.
  86.                         // get next event
  87.     e=[NXApp getNextEvent:(NX_MOUSEUPMASK | NX_MOUSEDRAGGEDMASK)];
  88.       } while( e->type!=NX_MOUSEUP);        // until mouse went up.
  89.       [self unlockFocus];
  90.       [[self window] setEventMask:saveMask];    // restore the event mask.
  91.     }
  92.   return self;
  93. }
  94.  
  95. - drawSelf:(NXRect *)r :(int)count
  96. {
  97.   int i;
  98.   PSsetlinecap( 1);                // rounded line caps.
  99.   PSsetgray( NX_LTGRAY);            // clear the background.
  100.   NXRectFill( &bounds);
  101.   PSsetlinewidth( lineWidth);            // set the line width.
  102.   PSsetgray( NX_BLACK);                // draw the line.
  103.   PSmoveto( p[ 0].x, p[ 0].y);
  104.   PScurveto( p[ 1].x, p[ 1].y, p[ 2].x, p[ 2].y, p[ 3].x, p[ 3].y);
  105.   PSstroke();
  106.   PSsetgray( NX_DKGRAY);
  107.   for( i=0; i<4; i++)
  108.     PSrectfill( p[ i].x-blockSize/2.0, p[ i].y-blockSize/2.0, blockSize, blockSize);
  109.   [[self window] flushWindow];
  110.   NXPing();
  111.   return self;
  112. }
  113.  
  114. @end
  115.